package de.gruessing.gwtsports.server.xmlmapper;

import java.util.ArrayList;
import java.util.List;

import de.gruessing.gwtsports.server.data.OriginalRootData;
import de.gruessing.gwtsports.server.data.tcdata.AbstractSourceT;
import de.gruessing.gwtsports.server.data.tcdata.AbstractStepT;
import de.gruessing.gwtsports.server.data.tcdata.ActivityLapT;
import de.gruessing.gwtsports.server.data.tcdata.ActivityListT;
import de.gruessing.gwtsports.server.data.tcdata.ActivityReferenceT;
import de.gruessing.gwtsports.server.data.tcdata.ActivityT;
import de.gruessing.gwtsports.server.data.tcdata.CourseFolderT;
import de.gruessing.gwtsports.server.data.tcdata.CourseLapT;
import de.gruessing.gwtsports.server.data.tcdata.CoursePointT;
import de.gruessing.gwtsports.server.data.tcdata.CourseT;
import de.gruessing.gwtsports.server.data.tcdata.CoursesT;
import de.gruessing.gwtsports.server.data.tcdata.ExtensionsT;
import de.gruessing.gwtsports.server.data.tcdata.FoldersT;
import de.gruessing.gwtsports.server.data.tcdata.HeartRateInBeatsPerMinuteT;
import de.gruessing.gwtsports.server.data.tcdata.HistoryFolderT;
import de.gruessing.gwtsports.server.data.tcdata.HistoryT;
import de.gruessing.gwtsports.server.data.tcdata.MultiSportFolderT;
import de.gruessing.gwtsports.server.data.tcdata.NameKeyReferenceT;
import de.gruessing.gwtsports.server.data.tcdata.PlanT;
import de.gruessing.gwtsports.server.data.tcdata.PositionT;
import de.gruessing.gwtsports.server.data.tcdata.QuickWorkoutT;
import de.gruessing.gwtsports.server.data.tcdata.TrackT;
import de.gruessing.gwtsports.server.data.tcdata.TrackpointT;
import de.gruessing.gwtsports.server.data.tcdata.TrainingCenterDatabaseT;
import de.gruessing.gwtsports.server.data.tcdata.TrainingT;
import de.gruessing.gwtsports.server.data.tcdata.WeekT;
import de.gruessing.gwtsports.server.data.tcdata.WorkoutFolderT;
import de.gruessing.gwtsports.server.data.tcdata.WorkoutT;
import de.gruessing.gwtsports.server.data.tcdata.WorkoutsT;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.ActivityDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.ActivityLapDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.ActivityListDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.ActivityReferenceDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.CourseDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.CourseFolderDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.CourseLapDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.CourseListDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.CoursePointDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.CoursesDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.ExtensionsDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.WrappedFoldersDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.HeartRateInBeatsPerMinuteDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.HistoryDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.HistoryFolderDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.MultiSportFolderDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.NameKeyReferenceDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.PlanDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.PositionDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.QuickWorkoutDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.SourceDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.StepDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.TrackDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.TrackpointDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.TrainingDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.TrainingDataDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.WeekDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.WorkoutDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.WorkoutFolderDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.WorkoutListDTO;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.WorkoutsDTO;

public class TCXMapper implements XMLMapper
{
	/**
	 * Maps JAXB generated beans to GWT-DTOs.
	 * @param in_xTD
	 * @return
	 */
	public TrainingDataDTO mapTrainingData(OriginalRootData in_xTD )
	{
		TrainingCenterDatabaseT xOriginalData = (TrainingCenterDatabaseT) in_xTD;
		TrainingDataDTO xMapped = new TrainingDataDTO();
		
		if( xOriginalData.getActivities() != null )
			xMapped.setActivities( mapActivities( xOriginalData.getActivities() ) );
		if( xOriginalData.getAuthor() != null )
			xMapped.setAuthor( mapAbstractSource( xOriginalData.getAuthor() ) );
		if( xOriginalData.getCourses() != null )			
			xMapped.setCourses( mapCourses( xOriginalData.getCourses().getCourse() ) );
		if( xOriginalData.getExtensions() != null )
			xMapped.setExtensions( mapExtensions( xOriginalData.getExtensions() ) );
		if( xOriginalData.getFolders() != null )
			xMapped.setFolders( mapFolders( xOriginalData.getFolders() ) );
		if( xOriginalData.getWorkouts() != null )
			xMapped.setWorkouts( mapWorkoutList( xOriginalData.getWorkouts().getWorkout() ) );

		return xMapped;
	}
	
	public WorkoutListDTO mapWorkoutList( List<WorkoutT> in_xWorkouts )
	{
		ArrayList<WorkoutDTO> xMappedWorkouts = new ArrayList<WorkoutDTO>();
		
		for( WorkoutT xOriginalWorkout : in_xWorkouts )
		{
			WorkoutDTO xNewWorkout = mapWorkout(xOriginalWorkout);
			xMappedWorkouts.add( xNewWorkout );
		}
		
		WorkoutListDTO xWorkoutList = new WorkoutListDTO();
		xWorkoutList.setWorkout(xMappedWorkouts);
		return xWorkoutList;
	}
	
	public WorkoutDTO mapWorkout( WorkoutT in_xWorkout )
	{
		WorkoutDTO xNewWorkout = new WorkoutDTO();
		
		if( in_xWorkout.getCreator() != null	)
			xNewWorkout.setCreator( mapAbstractSource( in_xWorkout.getCreator() ) );
		if( in_xWorkout.getExtensions() != null )
			xNewWorkout.setExtensions( mapExtensions( in_xWorkout.getExtensions() ) );
		if( in_xWorkout.getName() != null )
			xNewWorkout.setName( in_xWorkout.getName() );
		if( in_xWorkout.getNotes() != null)
			xNewWorkout.setNotes( in_xWorkout.getNotes() );
		if( in_xWorkout.getScheduledOn() != null )
			xNewWorkout.setScheduledOn( new ArrayList<String>( in_xWorkout.getScheduledOn() ) );
		if( in_xWorkout.getStep() != null )
			xNewWorkout.setStep( mapAbstractSteps( in_xWorkout.getStep() ) );
		
		return xNewWorkout;
	}
	
	public ArrayList<StepDTO> mapAbstractSteps( List<AbstractStepT> in_xSteps )
	{
		ArrayList<StepDTO> xMappedSteps = new ArrayList<StepDTO>();
		
		for( AbstractStepT xOriginalStep : in_xSteps )
		{
			StepDTO xNewStep = new StepDTO();
			xNewStep = mapAbstrractStep( xOriginalStep );
			xMappedSteps.add( xNewStep );
		}
		
		return xMappedSteps;
	}
	
	public StepDTO mapAbstrractStep( AbstractStepT in_xStep )
	{
		StepDTO xMappedStep = new StepDTO();
		xMappedStep.setStepId( in_xStep.getStepId() );
		return xMappedStep;
	}
	
	public WrappedFoldersDTO mapFolders( FoldersT in_xFolders )
	{
		WrappedFoldersDTO xMappedFolders = new WrappedFoldersDTO();
		
		if( in_xFolders.getCourses() != null )
			xMappedFolders.setCourses( mapCourses( in_xFolders.getCourses() ) );
		if( in_xFolders.getHistory() != null )
			xMappedFolders.setHistory( mapHistory( in_xFolders.getHistory() ) );
		if( in_xFolders.getWorkouts() != null )
			xMappedFolders.setWorkouts( mapWorkouts( in_xFolders.getWorkouts() ) );
		return xMappedFolders;
	}
	
	public WorkoutsDTO mapWorkouts( WorkoutsT in_xWorkouts )
	{
		WorkoutsDTO xMappedWorkouts = new WorkoutsDTO();
		
		if( in_xWorkouts.getBiking() != null )
			xMappedWorkouts.setBiking( mapWorkoutFolder( in_xWorkouts.getBiking() ) );
		if( in_xWorkouts.getExtensions() != null )
			xMappedWorkouts.setExtensions( mapExtensions( in_xWorkouts.getExtensions() ) );
		if( in_xWorkouts.getOther() != null)
			xMappedWorkouts.setOther( mapWorkoutFolder( in_xWorkouts.getOther() ) );
		if( in_xWorkouts.getRunning() != null )
			xMappedWorkouts.setRunning( mapWorkoutFolder( in_xWorkouts.getRunning() ) );
		return xMappedWorkouts;
	}
	
	public WorkoutFolderDTO mapWorkoutFolder( WorkoutFolderT in_xFolder )
	{
		WorkoutFolderDTO xMappedWorkoutFolder = new WorkoutFolderDTO();
		
		if( in_xFolder.getExtensions() != null )
			xMappedWorkoutFolder.setExtensions( mapExtensions( in_xFolder.getExtensions() ) );
		if( in_xFolder.getFolder() != null )
			xMappedWorkoutFolder.setFolder( mapWorkoutFolders( in_xFolder.getFolder() ) );
		if( in_xFolder.getName() != null )
			xMappedWorkoutFolder.setName( in_xFolder.getName() );
		if( in_xFolder.getWorkoutNameRef() != null )
			xMappedWorkoutFolder.setNameRef( mapNameKeyRefs( in_xFolder.getWorkoutNameRef() ) );
		
		return xMappedWorkoutFolder;
	}
	
	public ArrayList<WorkoutFolderDTO> mapWorkoutFolders( List<WorkoutFolderT> in_xFolders )
	{
		ArrayList<WorkoutFolderDTO> xMappedWorkoutFolders = new ArrayList<WorkoutFolderDTO>();
		
		for( WorkoutFolderT xOriginalFolder : in_xFolders )
		{
			WorkoutFolderDTO xNewFolder = mapWorkoutFolder( xOriginalFolder );
			xMappedWorkoutFolders.add( xNewFolder );
		}
		return xMappedWorkoutFolders;
	}
	
	public HistoryDTO mapHistory( HistoryT in_xHistory )
	{
		HistoryDTO xMappedHistory = new HistoryDTO();
		
		if( in_xHistory.getBiking() != null )
			xMappedHistory.setBiking( mapHistoryFolder( in_xHistory.getBiking() ) );
		if( in_xHistory.getExtensions() != null )
			xMappedHistory.setExtensions( mapExtensions( in_xHistory.getExtensions() ) );
		if( in_xHistory.getMultiSport() != null )
			xMappedHistory.setMultiSport( mapMultisportFolder( in_xHistory.getMultiSport() ) );
		if( in_xHistory.getOther() != null )
			xMappedHistory.setOther(mapHistoryFolder(in_xHistory.getOther()));
		if( in_xHistory.getRunning() != null )
			xMappedHistory.setRunning(mapHistoryFolder(in_xHistory.getRunning() ) );
		
		return xMappedHistory;
	}
	
	public MultiSportFolderDTO mapMultisportFolder( MultiSportFolderT in_xFolder )
	{
		MultiSportFolderDTO xMappedMultisportFolder = new MultiSportFolderDTO();
		
		if( in_xFolder.getExtensions() != null )
			xMappedMultisportFolder.setExtensions( mapExtensions( in_xFolder.getExtensions() ) );
		if( in_xFolder.getFolder() != null )
			xMappedMultisportFolder.setFolder( mapMultisportFolders( in_xFolder.getFolder() ) );
		if( in_xFolder.getMultisportActivityRef() != null )
			xMappedMultisportFolder.setMultisportActivityRef( mapActivityReferences( in_xFolder.getMultisportActivityRef() ) );
		if( in_xFolder.getName() != null )
			xMappedMultisportFolder.setName(in_xFolder.getName());
		if( in_xFolder.getNotes() != null )
			xMappedMultisportFolder.setNotes(in_xFolder.getNotes());
		if( in_xFolder.getWeek() != null )
			xMappedMultisportFolder.setWeek(mapWeeks(in_xFolder.getWeek()));
		
		return xMappedMultisportFolder;
	}
	
	public ArrayList<MultiSportFolderDTO> mapMultisportFolders( List<MultiSportFolderT> in_xFolders )
	{
		ArrayList<MultiSportFolderDTO> xMappedFolders = new ArrayList<MultiSportFolderDTO>();
		
		for( MultiSportFolderT xOriginalFolder : in_xFolders )
		{
			MultiSportFolderDTO xNewFolder = mapMultisportFolder(xOriginalFolder);
			xMappedFolders.add( xNewFolder );
		}
		return xMappedFolders;
	}
	
	public HistoryFolderDTO mapHistoryFolder( HistoryFolderT in_xHistoryFolder )
	{
		HistoryFolderDTO xMappedHistoryFolder = new HistoryFolderDTO();
		
		if( in_xHistoryFolder.getActivityRef() != null )
			xMappedHistoryFolder.setActivityRef( mapActivityReferences( in_xHistoryFolder.getActivityRef() ) );
		if( in_xHistoryFolder.getExtensions() != null )
			xMappedHistoryFolder.setExtensions( mapExtensions(in_xHistoryFolder.getExtensions()));
		if( in_xHistoryFolder.getFolder() != null )
			xMappedHistoryFolder.setFolder( mapHistoryFolders( in_xHistoryFolder.getFolder() ) );
		if( in_xHistoryFolder.getName() != null )
			xMappedHistoryFolder.setName( in_xHistoryFolder.getName() );
		if( in_xHistoryFolder.getNotes() != null )
			xMappedHistoryFolder.setNotes( in_xHistoryFolder.getNotes() );
		if( in_xHistoryFolder.getWeek() != null )
			xMappedHistoryFolder.setWeek( mapWeeks( in_xHistoryFolder.getWeek() ) );
		
		return xMappedHistoryFolder;
	}
	
	public WeekDTO mapWeek( WeekT in_xWeek )
	{
		WeekDTO xNewWeek = new WeekDTO();
		
		if( in_xWeek.getNotes() != null )
			xNewWeek.setNotes( in_xWeek.getNotes() );
		if( in_xWeek.getStartDay() != null )
			xNewWeek.setStartDay( in_xWeek.getStartDay() );
		
		return xNewWeek;
	}
	
	public ArrayList<WeekDTO> mapWeeks( List<WeekT> in_xWeeks )
	{
		ArrayList<WeekDTO> xMappedWeeks = new ArrayList<WeekDTO>();
		
		for( WeekT xOriginalWeek : in_xWeeks )
		{
			WeekDTO xNewWeek = mapWeek( xOriginalWeek );
			xMappedWeeks.add( xNewWeek );
		}
		
		return xMappedWeeks;
	}
	
	public ArrayList<HistoryFolderDTO> mapHistoryFolders( List<HistoryFolderT> in_xHistoryFolders )
	{
		ArrayList<HistoryFolderDTO> xMappedFolders = new ArrayList<HistoryFolderDTO>();
		
		for( HistoryFolderT xOriginalFolder : in_xHistoryFolders )
		{
			HistoryFolderDTO xNewFolder = mapHistoryFolder(xOriginalFolder);
			xMappedFolders.add( xNewFolder );
		}
		
		return xMappedFolders;
	}
	
	public ActivityReferenceDTO mapActivityRef( ActivityReferenceT in_xActivityRef )
	{
		ActivityReferenceDTO xMappedActivityRef = new ActivityReferenceDTO();
		
		if( in_xActivityRef.getId() != null )
			xMappedActivityRef.setId( in_xActivityRef.getId() );
		
		return xMappedActivityRef;
	}
	
	public ArrayList<ActivityReferenceDTO> mapActivityReferences( List<ActivityReferenceT> in_xActivityReferences )
	{
		ArrayList<ActivityReferenceDTO> xMappedActivityRefs = new ArrayList<ActivityReferenceDTO>();
		
		for( ActivityReferenceT xOriginalActivity : in_xActivityReferences )
		{
			ActivityReferenceDTO xNewActivityRef = new ActivityReferenceDTO();			
			xNewActivityRef.setId( xOriginalActivity.getId() );
			xMappedActivityRefs.add( xNewActivityRef );
		}
		return xMappedActivityRefs;
	}
	
	public CourseListDTO mapCourses( List<CourseT> in_xCourses )
	{
		ArrayList<CourseDTO> xMappedCourses = new ArrayList<CourseDTO>();
		
		for( CourseT xOriginalCourse : in_xCourses )
		{
			CourseDTO xNewCourse = mapCourse( xOriginalCourse );
			xMappedCourses.add(xNewCourse);
		}
		
		CourseListDTO xCourseListDTO = new CourseListDTO();
		xCourseListDTO.setCourse(xMappedCourses);
		return xCourseListDTO;
	}
	
	public CourseDTO mapCourse( CourseT in_xCourse )
	{
		CourseDTO xMappedCourse = new CourseDTO();
		
		if( in_xCourse.getCoursePoint() != null )
			xMappedCourse.setCoursePoint( mapCoursePoints( in_xCourse.getCoursePoint() ) );
		if( in_xCourse.getCreator() != null)
			xMappedCourse.setCreator( mapAbstractSource( in_xCourse.getCreator() ) );
		if( in_xCourse.getExtensions() != null )
			xMappedCourse.setExtensions( mapExtensions( in_xCourse.getExtensions() ) );
		if( in_xCourse.getLap() != null )
			xMappedCourse.setLap( mapCourseLaps( in_xCourse.getLap() ) );
		if( in_xCourse.getName() != null )
			xMappedCourse.setName( in_xCourse.getName() );
		if( in_xCourse.getNotes() != null )
			xMappedCourse.setNotes( in_xCourse.getNotes() );
		if( in_xCourse.getTrack() != null )
			xMappedCourse.setTrack( mapTracks( in_xCourse.getTrack() ) );
		
		return xMappedCourse;
	}
	
	public ArrayList<TrackDTO> mapTracks( List<TrackT> in_xTracks )
	{
		ArrayList<TrackDTO> xMappedTracks = new ArrayList<TrackDTO>();
		
		for( TrackT xOriginalTrack : in_xTracks )
		{
			TrackDTO xNewTrack = mapTrack(xOriginalTrack);
			xMappedTracks.add( xNewTrack );
		}
		return xMappedTracks;
	}
	
	public TrackDTO mapTrack( TrackT in_xTrack )
	{
		TrackDTO xMappedTrack = new TrackDTO();
		
		if( in_xTrack.getTrackpoint() != null )
			xMappedTrack.setTrackpoint( mapTrackpoints( in_xTrack.getTrackpoint() ) );
		
		return xMappedTrack;
	}
	
	public ArrayList<TrackpointDTO> mapTrackpoints( List<TrackpointT> in_xTrackpoints )
	{
		ArrayList<TrackpointDTO> xMappedTrackpoints = new ArrayList<TrackpointDTO>();		
		for( TrackpointT xOriginalTrackpoint : in_xTrackpoints )
		{
			TrackpointDTO xNewTrackpointDTO = mapTrackpoint( xOriginalTrackpoint );
			xMappedTrackpoints.add( xNewTrackpointDTO );
		}
		return xMappedTrackpoints;
	}
	
	public TrackpointDTO mapTrackpoint( TrackpointT in_xTrackpoint )
	{
		TrackpointDTO xMappedTrackpoint = new TrackpointDTO();
		
		xMappedTrackpoint.setAltitudeMeters( in_xTrackpoint.getAltitudeMeters() );
		xMappedTrackpoint.setCadence( in_xTrackpoint.getCadence() );
		xMappedTrackpoint.setDistanceMeters( in_xTrackpoint.getDistanceMeters() );
		if( in_xTrackpoint.getExtensions() != null )			
			xMappedTrackpoint.setExtensions( mapExtensions( in_xTrackpoint.getExtensions() ) );
		if( in_xTrackpoint.getHeartRateBpm() != null )
			xMappedTrackpoint.setHeartRateBpm( mapHeartRateBPM( in_xTrackpoint.getHeartRateBpm() ) );
		if( in_xTrackpoint.getPosition() != null )
			xMappedTrackpoint.setPosition( mapPosition( in_xTrackpoint.getPosition() ) );
		if( in_xTrackpoint.getTime() != null )
		xMappedTrackpoint.setTime( in_xTrackpoint.getTime() );
		
		return xMappedTrackpoint;
	}
	
	public ArrayList<CourseLapDTO> mapCourseLaps( List<CourseLapT> in_xLaps )
	{
		ArrayList<CourseLapDTO> xMappedCourseLaps = new ArrayList<CourseLapDTO>();
		
		for( CourseLapT xOriginalLap : in_xLaps )
		{
			CourseLapDTO xNewLap = mapCourseLap(xOriginalLap);
			xMappedCourseLaps.add( xNewLap );
		}
		return xMappedCourseLaps;
	}
	
	public CourseLapDTO mapCourseLap( CourseLapT in_xCourseLap )
	{
		CourseLapDTO xMappedCourseLap = new CourseLapDTO();
		
		if( in_xCourseLap.getAverageHeartRateBpm() != null )
			xMappedCourseLap.setAverageHeartRateBpm( mapHeartRateBPM( in_xCourseLap.getAverageHeartRateBpm() ) );
		xMappedCourseLap.setBeginAltitudeMeters( in_xCourseLap.getBeginAltitudeMeters() );
		if( in_xCourseLap.getBeginPosition() != null )
			xMappedCourseLap.setBeginPosition( mapPosition( in_xCourseLap.getBeginPosition() ) );
		xMappedCourseLap.setCadence( in_xCourseLap.getCadence() );
		xMappedCourseLap.setDistanceMeters( in_xCourseLap.getDistanceMeters() );
		xMappedCourseLap.setEndAltitudeMeters( in_xCourseLap.getEndAltitudeMeters() );
		if( in_xCourseLap.getEndPosition() != null )
			xMappedCourseLap.setEndPosition( mapPosition( in_xCourseLap.getEndPosition() ) );
		if( in_xCourseLap.getExtensions() != null )
			xMappedCourseLap.setExtensions( mapExtensions( in_xCourseLap.getExtensions() ) );
		if( in_xCourseLap.getMaximumHeartRateBpm() != null )
			xMappedCourseLap.setMaximumHeartRateBpm( mapHeartRateBPM( in_xCourseLap.getMaximumHeartRateBpm() ) );
		xMappedCourseLap.setTotalTimeSeconds( in_xCourseLap.getTotalTimeSeconds() );
		
		return xMappedCourseLap;
	}	
	
	public ArrayList<CoursePointDTO> mapCoursePoints( List<CoursePointT> in_xCoursePoints )
	{
		ArrayList<CoursePointDTO> xMappedCoursePoints = new ArrayList<CoursePointDTO>();
		
		for( CoursePointT xOriginalPoint : in_xCoursePoints )
		{
			CoursePointDTO xNewCoursePoint = mapCoursePoint(xOriginalPoint);
			xMappedCoursePoints.add(xNewCoursePoint);
		}
		
		return xMappedCoursePoints;
	}
	
	public CoursePointDTO mapCoursePoint( CoursePointT in_xCoursePoint )
	{
		CoursePointDTO xMappedCoursePoint = new CoursePointDTO();
		
		xMappedCoursePoint.setAltitudeMeters( in_xCoursePoint.getAltitudeMeters() );
		if( in_xCoursePoint.getExtensions() != null)
			xMappedCoursePoint.setExtensions( mapExtensions( in_xCoursePoint.getExtensions() ) );
		if( in_xCoursePoint.getName() != null )
			xMappedCoursePoint.setName( in_xCoursePoint.getName() );
		if( in_xCoursePoint.getNotes() != null )
			xMappedCoursePoint.setNotes( in_xCoursePoint.getNotes() );
		if( in_xCoursePoint.getPointType() != null )
			xMappedCoursePoint.setPointType( in_xCoursePoint.getPointType() );
		if( in_xCoursePoint.getPosition() != null )
			xMappedCoursePoint.setPosition( mapPosition( in_xCoursePoint.getPosition() ) );
		if( in_xCoursePoint.getTime() != null )
		in_xCoursePoint.setTime( in_xCoursePoint.getTime() );
		
		return xMappedCoursePoint;
	}
	
	public PositionDTO mapPosition( PositionT in_xPosition )
	{
		PositionDTO xMappedPosition = new PositionDTO();
		
		xMappedPosition.setLatitudeDegrees( in_xPosition.getLatitudeDegrees() );
		xMappedPosition.setLongitudeDegrees(in_xPosition.getLongitudeDegrees() );
		return xMappedPosition;
	}
	
	public ActivityListDTO mapActivities( ActivityListT in_xActListT )
	{		
		ActivityListDTO xActListDTO = new ActivityListDTO();
		
		if( in_xActListT.getActivity() != null )
		{
			ArrayList<ActivityDTO> xMappedActivityList = new ArrayList<ActivityDTO>();
			
			for( ActivityT xOrgActivity : in_xActListT.getActivity() )
			{
				ActivityDTO xActDTO = new ActivityDTO();
				
				if( xOrgActivity.getCreator() != null )
					xActDTO.setCreator( mapAbstractSource( xOrgActivity.getCreator() ) );
				
				if( xOrgActivity.getExtensions() != null )
					xActDTO.setExtensions( mapExtensions( xOrgActivity.getExtensions() ) );				
				
				if( xOrgActivity.getId() != null )
					xActDTO.setId( xOrgActivity.getId() );
				
				if( xOrgActivity.getLap() != null )
					xActDTO.setLap( mapLap( xOrgActivity.getLap() ) );
				
				if( xOrgActivity.getNotes() != null )
					xActDTO.setNotes( xOrgActivity.getNotes() );
				
				if( xOrgActivity.getTraining() != null )
					xActDTO.setTraining( mapTraining( xOrgActivity.getTraining() ) );
				
				xMappedActivityList.add( xActDTO );
			}
			
			xActListDTO.setActivity(xMappedActivityList);
		}		
		return xActListDTO; 
	}
	
	public SourceDTO mapAbstractSource( AbstractSourceT in_xAbstSource )
	{
		SourceDTO xSource = new SourceDTO();
		xSource.setName(in_xAbstSource.getName());
		return xSource;
	}
	
	public ExtensionsDTO mapExtensions( ExtensionsT in_xExtentions )
	{
		ExtensionsDTO xExtensions = new ExtensionsDTO();
		
		if( in_xExtentions != null )
		{			
			ArrayList<String> xMappedObjects = new ArrayList<String>();			
			for( Object xObject : in_xExtentions.getAny() )
			{
				if( xObject instanceof String )
				{
					xMappedObjects.add( (String) xObject );
				}
			}
			xExtensions.setAny(xMappedObjects);
		}		
		return xExtensions;
	}
	
	public ArrayList<ActivityLapDTO> mapLap( List<ActivityLapT> in_xOriginalLaps )
	{
		ArrayList<ActivityLapDTO> xMappedLaps = new ArrayList<ActivityLapDTO>();
		
		for( ActivityLapT xOriginalLap : in_xOriginalLaps )
		{
			ActivityLapDTO xMappedLap = new ActivityLapDTO();
			
			if( xOriginalLap.getAverageHeartRateBpm() != null )
				xMappedLap.setAverageHeartRateBpm( mapHeartRateBPM( xOriginalLap.getAverageHeartRateBpm() ) );
			xMappedLap.setCadence( xOriginalLap.getCadence() );
			xMappedLap.setCalories( xOriginalLap.getCalories() );
			xMappedLap.setDistanceMeters( xOriginalLap.getDistanceMeters() );
			if( xOriginalLap.getExtensions() != null )
				xMappedLap.setExtensions( mapExtensions( xOriginalLap.getExtensions() ) );
			if( xOriginalLap.getMaximumHeartRateBpm() != null )
			xMappedLap.setMaximumHeartRateBpm( mapHeartRateBPM( xOriginalLap.getMaximumHeartRateBpm() ) );
			xMappedLap.setMaximumSpeed( xOriginalLap.getMaximumSpeed() );
			xMappedLap.setNotes( xOriginalLap.getNotes() );
			xMappedLap.setStartTime( xOriginalLap.getStartTime() );
			xMappedLap.setTotalTimeSeconds( xOriginalLap.getTotalTimeSeconds() );
			xMappedLaps.add(xMappedLap);
		}
		return xMappedLaps;
	}
	
	public HeartRateInBeatsPerMinuteDTO mapHeartRateBPM( HeartRateInBeatsPerMinuteT in_xOrgHeartRate )
	{
		HeartRateInBeatsPerMinuteDTO xMappedHeartRate = new HeartRateInBeatsPerMinuteDTO();		
		if( in_xOrgHeartRate != null )
			xMappedHeartRate.setValue( in_xOrgHeartRate.getValue() );
		return xMappedHeartRate;
	}
	
	public TrainingDTO mapTraining( TrainingT in_xTraining )
	{
		TrainingDTO xMappedTraining = new TrainingDTO();
		
		if( in_xTraining.getPlan() != null )
			xMappedTraining.setPlan( mapPlan( in_xTraining.getPlan() ) );
		
		if( in_xTraining.getQuickWorkoutResults() != null )
			xMappedTraining.setQuickWorkoutResults( mapQuickWorkout( in_xTraining.getQuickWorkoutResults() ));
		
		return xMappedTraining;
	}
	
	public PlanDTO mapPlan( PlanT in_xPlan )
	{
		PlanDTO xMappedPlan = new PlanDTO();
		
		if( in_xPlan.getExtensions() != null)
			xMappedPlan.setExtensions(mapExtensions(in_xPlan.getExtensions()));		
		xMappedPlan.setName( in_xPlan.getName() );
		
		return xMappedPlan;
	}
	
	public QuickWorkoutDTO mapQuickWorkout( QuickWorkoutT in_xWorkout )
	{
		QuickWorkoutDTO xMappedWorkout = new QuickWorkoutDTO();
		xMappedWorkout.setDistanceMeters( in_xWorkout.getDistanceMeters() );
		xMappedWorkout.setTotalTimeSeconds(in_xWorkout.getTotalTimeSeconds());
		return xMappedWorkout;
	}		
	
	public CoursesDTO mapCourses( CoursesT in_xCourses )
	{
		CoursesDTO xMappedCourses = new CoursesDTO();
		
		if( in_xCourses.getCourseFolder() != null )
			xMappedCourses.setCourseFolder( mapCourseFolder( in_xCourses.getCourseFolder() ) );
		
		if( in_xCourses.getExtensions() != null )
			xMappedCourses.setExtensions( mapExtensions( in_xCourses.getExtensions() ) );
		
		return xMappedCourses;
	}
	
	public CourseFolderDTO mapCourseFolder( CourseFolderT in_xCourseFolder )
	{
		CourseFolderDTO xMappedCourseFolder = new CourseFolderDTO();
		
		if( in_xCourseFolder.getCourseNameRef() != null )
			xMappedCourseFolder.setNameRef( mapNameKeyRefs( in_xCourseFolder.getCourseNameRef() ) );
		
		if( in_xCourseFolder.getExtensions() != null )
			xMappedCourseFolder.setExtensions( mapExtensions( in_xCourseFolder.getExtensions() ) );
		
		if( in_xCourseFolder.getFolder() != null )
			xMappedCourseFolder.setFolder( mapCourseFolders( in_xCourseFolder.getFolder() ) );
		
		if( in_xCourseFolder.getName() != null )
			xMappedCourseFolder.setName(in_xCourseFolder.getName());
		
		if( in_xCourseFolder.getNotes() != null )
			xMappedCourseFolder.setNotes(in_xCourseFolder.getNotes());
		
		return xMappedCourseFolder;
	}		
	
	public ArrayList<NameKeyReferenceDTO> mapNameKeyRefs( List<NameKeyReferenceT> in_xNameKeyRefs )
	{
		ArrayList<NameKeyReferenceDTO> xMappedNameKeyRefs = new ArrayList<NameKeyReferenceDTO>();
		for( NameKeyReferenceT xOrgNameKeyReference : in_xNameKeyRefs )
		{
			NameKeyReferenceDTO xNameKeyRef = new NameKeyReferenceDTO();		
			xNameKeyRef.setId( xOrgNameKeyReference.getId() );
			xMappedNameKeyRefs.add( xNameKeyRef );
		}
		return xMappedNameKeyRefs;
	}
	
	public NameKeyReferenceDTO mapNameKeyRef( NameKeyReferenceT in_xNameKeyRef )
	{
		NameKeyReferenceDTO xNameKeyRef = new NameKeyReferenceDTO();
		xNameKeyRef.setId( in_xNameKeyRef.getId() );
		return xNameKeyRef;
	}
	
	public ArrayList<CourseFolderDTO> mapCourseFolders( List<CourseFolderT> in_xCourseFolders )
	{
		ArrayList<CourseFolderDTO> xMappedCourseFolders = new ArrayList<CourseFolderDTO>();
		
		for( CourseFolderT xOriginalCourseFolder : in_xCourseFolders )
		{
			CourseFolderDTO xCourseFolder = mapCourseFolder( xOriginalCourseFolder );
			xMappedCourseFolders.add(xCourseFolder);
		}
		
		return xMappedCourseFolders;
	}
}